package org.example.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.example.springboot.common.page.PageResult;
import org.example.springboot.common.service.impl.BaseServiceImpl;
import org.example.springboot.convert.IrrigationLogConvert;
import org.example.springboot.dao.IrrigationLogDao;
import org.example.springboot.entity.IrrigationLog;
import org.example.springboot.query.IrrigationLogQuery;
import org.example.springboot.service.IrrigationLogService;
import org.example.springboot.vo.IrrigationLogVO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Fz
 * @since 1.0.0 2024-08-01
 */
@Service
@AllArgsConstructor
public class IrrigationLogServiceImpl extends BaseServiceImpl<IrrigationLogDao, IrrigationLog> implements IrrigationLogService {

    @Override
    public PageResult<IrrigationLogVO> page(IrrigationLogQuery query) {
        IPage<IrrigationLog> page = this.page(query, getWrapper(query));
        return new PageResult<>(IrrigationLogConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<IrrigationLog> getWrapper(IrrigationLogQuery query) {
        LambdaQueryWrapper<IrrigationLog> wrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(query.getStartDate()) && StringUtils.isNotBlank(query.getEndDate())) {
            wrapper.between(IrrigationLog::getCreateDate, query.getStartDate(), query.getEndDate());
        }
        if (StringUtils.isNotBlank(query.getOperationType())) {
            wrapper.eq(IrrigationLog::getOperationType, query.getOperationType());
        }
        wrapper.orderByDesc(IrrigationLog::getCreateDate);
        return wrapper;
    }

    @Override
    public void delete(List<String> ids) {
        this.removeByIds(ids);
    }

}